<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_News_Models_Mysql_Article extends Modules_News_Models_Article
{	
	protected $_table = 'news_article';
	
	public function addToCategories($articleId, $categoryIds)
	{
		$sql = sprintf("DELETE FROM " . $this->_conn->_tablePrefix . "news_article_category_assoc WHERE article_id = '%s'", 
					$this->_conn->escape($articleId));
		$this->_conn->query($sql);
		
		if (is_array($categoryIds)) {
			foreach ($categoryIds as $categoryId) {
				$sql = sprintf("INSERT INTO " . $this->_conn->_tablePrefix . "news_article_category_assoc (category_id, article_id)
								VALUES ('%s', '%s')",
								$this->_conn->escape($categoryId),
								$this->_conn->escape($articleId));
				$this->_conn->query($sql);
			}
		}
	}
	
	public function addToRelateArticles($articleId, $relateArticleIds)
	{
		$sql = sprintf("DELETE FROM " . $this->_conn->_tablePrefix . "news_article_relate_assoc WHERE article_id = '%s'", 
					$this->_conn->escape($articleId));
		$this->_conn->query($sql);
		
		if (is_array($relateArticleIds)) {
			foreach ($relateArticleIds as $relateArticleId) {
				$sql = sprintf("INSERT INTO " . $this->_conn->_tablePrefix . "news_article_relate_assoc (relate_article_id, article_id)
								VALUES ('%s', '%s')",
								$this->_conn->escape($relateArticleId),
								$this->_conn->escape($articleId));
				$this->_conn->query($sql);
			}
		}
	}
	
	public function getCategoryIds($articleId)
	{
		$sql = sprintf("SELECT category_id 
						FROM " . $this->_conn->_tablePrefix . "news_article_category_assoc AS a
						WHERE a.article_id = '%s'",
						$this->_conn->escape($articleId));

		$rs = $this->_conn->query($sql);
		$categoryIds = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$categoryIds[] = $row['category_id'];
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $categoryIds;				
	}
	
	public function getById($articleId, $active = false)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'news_article 
							WHERE article_id = ' . $this->_conn->escape($articleId);
		if ($active) {
			$sql .= ' AND status = "active"';
		}
		$sql .= ' LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		$this->_conn->freeResult($rs);
		return $return;
	}

	public function getByTitle($title)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'news_article 
							WHERE title = "' . $this->_conn->escape($title) . '" LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		$this->_conn->freeResult($rs);
		return $return;
	}
	
	public function getByLink($link)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE link_source = "' . $this->_conn->escape($link) . '" LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		$this->_conn->freeResult($rs);
		return $return;
	}
	
	public function getBySlug($slug)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'news_article 
					WHERE slug = "' . $this->_conn->escape($slug) . '" LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		$this->_conn->freeResult($rs);
		return $return;
	}
	
	public function increaseViews($articleId)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . 'news_article 
					SET num_viewed = num_viewed + 1 
					WHERE article_id = "%s"';
		$sql = sprintf($sql, $this->_conn->escape($articleId));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();	
	}
	
	public function getArticlesByTagId($tagId, $offset = null, $limit = null)
	{
		$sql = 'SELECT a.*, ws.name AS web_name FROM ' . $this->_conn->_tablePrefix . 'news_article AS a
				INNER JOIN ' . $this->_conn->_tablePrefix . 'news_tag_assoc AS at
					ON a.article_id = at.article_id 
				LEFT JOIN ' . $this->_conn->_tablePrefix . 'news_web_source AS ws
					ON a.web_id = ws.web_id	
				WHERE a.status = "active" AND at.tag_id = ' . (int)$tagId;
		$sql .= ' ORDER BY a.created_date DESC, a.article_id DESC';
		if ($offset !== false && $limit !== false) {
			$sql .= ' LIMIT ' . (int)$offset . ',' . (int)$limit;
		}

		$rs = $this->_conn->query($sql);
		$articles = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$articles[] = $this->format($row);
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $articles;
	}
	
	public function countArticlesByTagId($tagId)
	{
		$sql = 'SELECT COUNT(*) as num_articles FROM ' . $this->_conn->_tablePrefix . 'news_article AS a
				INNER JOIN ' . $this->_conn->_tablePrefix . 'news_tag_assoc AS at
					ON a.article_id = at.article_id
				LEFT JOIN ' . $this->_conn->_tablePrefix . 'news_web_source AS ws
					ON a.web_id = ws.web_id			
				WHERE a.status = "active" AND at.tag_id = ' . (int)$tagId;
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $row['num_articles'];
	}
	
	public function add($article)
	{
		return $this->_conn->insert($article, $this->_conn->_tablePrefix . 'news_article');	
	}
	
	public function find($offset = null, $limit = null, $condition = array())
	{
		$sql = 'SELECT a.* FROM ' . $this->_conn->_tablePrefix . 'news_article AS a
				WHERE a.language = "' . $this->_conn->escape($this->getLang()) . '"';

		if (isset($condition['category_id']) && $condition['category_id']) {
			$sql .= ' AND a.article_id IN (SELECT article_id FROM ' . $this->_conn->_tablePrefix . 'news_article_category_assoc AS acs 
										WHERE acs.category_id = "' . $this->_conn->escape($condition['category_id']) . '")';
		}
		if (isset($condition['status']) && $condition['status']) {
			$sql .= ' AND a.status = "' . $this->_conn->escape($condition['status']) . '"';
		}
		if (isset($condition['keyword']) && $condition['keyword']) {
			$sql .= ' AND (a.title LIKE "%' . $this->_conn->escape($condition['keyword']) . '%"';
			//$sql .= ' 	OR a.description LIKE "%' . $this->_conn->escape($condition['keyword']) . '%"';
			//$sql .= ' 	OR a.content LIKE "%' . $this->_conn->escape($condition['keyword']) . '%"';
			
			$keywordNoSign = Gio_Core_String::removeSign($condition['keyword'], '-', true);
			//$sql .= ' 	OR a.slug LIKE "%' . $this->_conn->escape($keywordNoSign) . '%"';
			
			$sql .= ')';
		}
		if (isset($condition['article_id']) && $condition['article_id']) {
			$sql .= ' AND a.article_id = "' . $this->_conn->escape($condition['article_id']) . '"';
		}
		if (isset($condition['article_hot'])) {
			$sql .= ' AND a.article_hot = ' . (int)$condition['article_hot'];
		}
		if (isset($condition['article_sticky']) && $condition['article_sticky'] == 1) {
			$sql .= ' AND a.article_sticky = 1';
		}
		if (isset($condition['article_photo']) && $condition['article_photo'] == 1) {
			$sql .= ' AND a.article_photo = 1';
		}
		if (isset($condition['created_date_from']) && $condition['created_date_from']) {
			$sql .= ' AND a.created_date >= "' . $this->_conn->escape($condition['created_date_from']) . '"';
		}
		if (isset($condition['created_date_to']) && $condition['created_date_to']) {
			$sql .= ' AND a.created_date <= "' . $this->_conn->escape($condition['created_date_to']) . '"';
		}
		if (isset($condition['web_id']) && $condition['web_id']) {
			$sql .= ' AND a.web_id = "' . $this->_conn->escape($condition['web_id']) . '"';
		}
		if (isset($condition['user_id']) && $condition['user_id']) {
			$sql .= ' AND a.user_id = "' . $this->_conn->escape($condition['user_id']) . '"';
		}
		
		$sql .= ' ORDER BY a.created_date DESC, a.article_id DESC';
		if ($offset !== false && $limit !== false) {
			$sql .= ' LIMIT ' . (int)$offset . ',' . (int)$limit;
		}

		$rs = $this->_conn->query($sql);
		$articles = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$articles[] = $this->format($row);
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $articles;
	}
	
	public function count($condition = array())
	{
		/**
		 * Language is index column 
		 */
		$sql = 'SELECT COUNT(a.language) as num_articles FROM ' . $this->_conn->_tablePrefix . 'news_article AS a
				WHERE a.language = "' . $this->_conn->escape($this->getLang()) . '"';	

		if (isset($condition['category_id']) && $condition['category_id']) {
			$sql .= ' AND a.article_id IN (SELECT article_id FROM ' . $this->_conn->_tablePrefix . 'news_article_category_assoc AS acs 
										WHERE acs.category_id = "' . $this->_conn->escape($condition['category_id']) . '")';
		}
		if (isset($condition['status']) && $condition['status']) {
			$sql .= ' AND a.status = "' . $this->_conn->escape($condition['status']) . '"';
		}
		if (isset($condition['keyword']) && $condition['keyword']) {
			$sql .= ' AND (a.title LIKE "%' . $this->_conn->escape($condition['keyword']) . '%"';
			//$sql .= ' 	OR a.description LIKE "%' . $this->_conn->escape($condition['keyword']) . '%"';
			//$sql .= ' 	OR a.content LIKE "%' . $this->_conn->escape($condition['keyword']) . '%"';
			
			$keywordNoSign = Gio_Core_String::removeSign($condition['keyword'], '-', true);
			//$sql .= ' 	OR a.slug LIKE "%' . $this->_conn->escape($keywordNoSign) . '%"';
			
			$sql .= ')';
		}
		if (isset($condition['article_id']) && $condition['article_id']) {
			$sql .= ' AND a.article_id = "' . $this->_conn->escape($condition['article_id']) . '"';
		}
		if (isset($condition['article_hot'])) {
			$sql .= ' AND a.article_hot = ' . (int)$condition['article_hot'];
		}
		if (isset($condition['article_sticky']) && $condition['article_sticky'] == 1) {
			$sql .= ' AND a.article_sticky = 1';
		}
		if (isset($condition['article_photo']) && $condition['article_photo'] == 1) {
			$sql .= ' AND a.article_photo = 1';
		}
		if (isset($condition['created_date_from']) && $condition['created_date_from']) {
			$sql .= ' AND a.created_date >= "' . $this->_conn->escape($condition['created_date_from']) . '"';
		}
		if (isset($condition['created_date_to']) && $condition['created_date_to']) {
			$sql .= ' AND a.created_date <= "' . $this->_conn->escape($condition['created_date_to']) . '"';
		}
		if (isset($condition['web_id']) && $condition['web_id']) {
			$sql .= ' AND a.web_id = "' . $this->_conn->escape($condition['web_id']) . '"';
		}
		if (isset($condition['user_id']) && $condition['user_id']) {
			$sql .= ' AND a.user_id = "' . $this->_conn->escape($condition['user_id']) . '"';
		}
		
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $row['num_articles'];
	}
	
	public function update($article)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . $this->_table . '  
					SET title = "%s", slug = "%s", description = "%s", content = "%s",
						status = "%s", category_id = "%s", image_url = "%s", image_url_data = "%s",
						article_hot = "%s", article_photo = "%s", article_video = "%s",
						web_id = "%s", link_source = "%s", author = "%s"
					WHERE article_id = "%s"';
		$sql = sprintf($sql, $this->_conn->escape($article['title']),
							$this->_conn->escape($article['slug']),
							$this->_conn->escape($article['description']),
							$this->_conn->escape($article['content']),
							$this->_conn->escape($article['status']),
							$this->_conn->escape($article['category_id']),
							$this->_conn->escape($article['image_url']),
							$this->_conn->escape($article['image_url_data']),
							$this->_conn->escape($article['article_hot']),
							$this->_conn->escape($article['article_photo']),
							$this->_conn->escape($article['article_video']),
							$this->_conn->escape($article['web_id']),
							$this->_conn->escape($article['link_source']),
							$this->_conn->escape($article['author']),
							$this->_conn->escape($article['article_id']));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();	
	}
	
	public function delete($articleId)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . 'news_article 
							WHERE article_id = ' . $this->_conn->escape($articleId);
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function updateStatus($articleId, $status)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . 'news_article 
					SET status = "%s" 
					WHERE article_id = "%s"';
		$sql = sprintf($sql, $this->_conn->escape($status),
							$this->_conn->escape($articleId));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();
	}
	
	public function getHotArticle($limit = 5, $categoryId = null)
	{
		$limit = ($limit) ? $limit : 5;
		$sql = 'SELECT a.* FROM ' . $this->_conn->_tablePrefix . 'news_article AS a';
		if ($categoryId) {
			$sql .= ' INNER JOIN ' . $this->_conn->_tablePrefix . 'news_article_category_assoc AS acs
					ON a.article_id = acs.article_id';
		}
		$sql .= ' WHERE a.article_hot = 1 AND a.status = "active" AND (image_url != "" OR image_url_data != "")';
		if ($categoryId) {
			$sql .= ' AND (acs.category_id = ' . $this->_conn->escape($categoryId) .' OR a.category_id = ' . $this->_conn->escape($categoryId) .')';
			$sql .= ' GROUP BY a.article_id';
		}
		$sql .= ' ORDER BY a.created_date DESC, a.article_id DESC
					LIMIT ' . $limit;
		$rs = $this->_conn->query($sql);
		$articles = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$articles[] = $this->format($row);
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $articles;
	}
	
	public function getPhotoArticles($limit = 5)
	{
		$limit = ($limit) ? $limit : 5;
		$sql = 'SELECT DISTINCT(a.slug), a.* FROM ' . $this->_conn->_tablePrefix . 'news_article AS a 
					WHERE a.article_photo = 1 AND a.status = "active"
				ORDER BY a.created_date DESC, a.article_id DESC
				LIMIT ' . $limit;
		$rs = $this->_conn->query($sql);
		$articles = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$articles[] = $this->format($row);
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $articles;
	}
	
	public function getMostViewedArticle($categoryId = null, $limit = 5)
	{
		$limit = ($limit) ? $limit : 5;
		$sql = 'SELECT a.* FROM ' . $this->_conn->_tablePrefix . 'news_article AS a 
					WHERE a.status = "active" AND a.num_viewed >= 0 ';
		if ($categoryId) {
			$sql .= 'AND a.category_id = ' . $this->_conn->escape($categoryId);
		}
		$sql .= ' ORDER BY a.num_viewed DESC LIMIT ' . $limit;
		$rs = $this->_conn->query($sql);
		$articles = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$articles[] = $this->format($row);
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $articles;
	}
	
	public function getStickyArticle($categoryId = null, $limit = 5)
	{
		$limit = ($limit) ? $limit : 5;
		$sql = 'SELECT a.* FROM ' . $this->_conn->_tablePrefix . 'news_article AS a
					WHERE a.status = "active" AND a.article_sticky = 1 ';
		if ($categoryId) {
			$sql .= 'AND a.category_id = ' . $this->_conn->escape($categoryId);
		}
		$sql .= ' ORDER BY a.created_date DESC, a.article_id DESC
				  LIMIT ' . $limit;
		$rs = $this->_conn->query($sql);
		$articles = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$articles[] = $this->format($row);
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $articles;
	}
	
	public function getLatestArticle($categoryId = null, $limit = 5)
	{
		$limit = ($limit) ? $limit : 5;
		$sql = 'SELECT a.* FROM ' . $this->_conn->_tablePrefix . 'news_article AS a
					WHERE a.status = "active"';
		if ($categoryId) {   
			$sql .= ' AND a.category_id = ' . $this->_conn->escape($categoryId);
		}
		$sql .= ' ORDER BY a.created_date DESC
					LIMIT ' . $this->_conn->escape($limit);
		$rs = $this->_conn->query($sql);
		$articles = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$articles[] = $this->format($row);
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $articles;
	}
	
	public function getOlderArticles($article, $limit = 5, $categoryId = null)
	{
		if (null == $article) {
			return null;
		}
		
		$limit = ($limit) ? $limit : 5;
		$sql = 'SELECT a.* FROM ' . $this->_conn->_tablePrefix . $this->_table . ' AS a
				WHERE a.article_id <> "' . $this->_conn->escape($article['article_id']) . '"
							AND a.created_date <= "' . $this->_conn->escape($article['created_date']) . '"
							AND a.status = "active"';
		if ($categoryId) {
			$sql .= ' AND a.category_id = ' . $this->_conn->escape($categoryId);
		}
		$sql .= ' ORDER BY a.created_date DESC, a.article_id DESC
					LIMIT ' . $this->_conn->escape($limit);
		$rs = $this->_conn->query($sql);
		$articles = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$articles[] = $this->format($row);
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $articles;
	}
	
	public function getNewerArticles($article, $limit = 5, $categoryId = null)
	{
		if (null == $article) {
			return null;
		}
		
		$limit = ($limit) ? $limit : 5;
		$sql = 'SELECT a.* FROM ' . $this->_conn->_tablePrefix . 'news_article AS a
				WHERE a.article_id <> "' . $this->_conn->escape($article['article_id']) . '"
							AND a.created_date > "' . $this->_conn->escape($article['created_date']) . '"
							AND a.status = "active"';
		if ($categoryId) {
			$sql .= ' AND a.category_id = ' . $this->_conn->escape($categoryId);
		}
		$sql .= ' ORDER BY a.created_date DESC, a.article_id DESC
					LIMIT ' . $this->_conn->escape($limit);
		$rs = $this->_conn->query($sql);
		$articles = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$articles[] = $this->format($row);
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $articles;
	}
	
	public function newsOfDay($date, $offset = null, $limit = null)
	{
		$sql = 'SELECT a.*, ws.name AS web_name FROM ' . $this->_conn->_tablePrefix . 'news_article AS a
				LEFT JOIN ' . $this->_conn->_tablePrefix . 'news_web_source AS ws
					ON a.web_id = ws.web_id
				WHERE a.status = "active" AND a.created_date LIKE "' . $this->_conn->escape($date) . ' %"';
		
		$sql .= ' ORDER BY a.created_date DESC, a.article_id DESC';
		if ($offset !== false && $limit !== false) {
			$sql .= ' LIMIT ' . (int)$offset . ',' . (int)$limit;
		}

		$rs = $this->_conn->query($sql);
		$articles = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$articles[] = $this->format($row);
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $articles;
	}
	
	public function countNewsOfDay($date)
	{
		$sql = 'SELECT COUNT(article_id) AS num_articles FROM ' . $this->_conn->_tablePrefix . 'news_article 
				WHERE status = "active" AND created_date LIKE "' . $this->_conn->escape($date) . ' %" 
				LIMIT 1';
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $row['num_articles'];
	}
	
	/* ========== For translation =========================================== */
	
	public function getTranslations($item, $lang = null)
	{
		$sql  = sprintf("SELECT a.* FROM " . $this->_conn->_tablePrefix . $this->_table . " AS a
						INNER JOIN 
						(
							SELECT tr1.* FROM " . $this->_conn->_tablePrefix . "core_translation AS tr1
							INNER JOIN " . $this->_conn->_tablePrefix . "core_translation AS tr2 
								ON (tr1.item_id = '%s' AND tr1.source_item_id = tr2.item_id) 
								OR (tr2.item_id = '%s' AND tr1.item_id = tr2.source_item_id)
								OR (tr1.source_item_id = '%s' AND tr1.source_item_id = tr2.source_item_id)
							WHERE tr1.item_class = '%s' AND tr2.item_class = '%s'
							GROUP by tr1.translation_id
						) AS tr
							ON tr.item_id = a.article_id", 
						$this->_conn->escape($item['article_id']), 
						$this->_conn->escape($item['article_id']), 
						$this->_conn->escape($item['article_id']),
						'Modules_News_Models_Article',
						'Modules_News_Models_Article');
		$rs = $this->_conn->query($sql);
		$rows = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$rows[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $rows;
	}

	public function getTranslatable($lang, $count, $selected = null)
	{
		$sql  = sprintf('SELECT a.*, (tr.item_id IS NULL) AS translatable 
						FROM ' . $this->_conn->_tablePrefix . $this->_table . ' AS a
						LEFT JOIN ' . $this->_conn->_tablePrefix . 'core_translation AS tr
							ON tr.source_item_id = a.article_id
							AND tr.item_class = "%s"
							AND tr.language = "%s"
						WHERE a.language = "%s"%s
						ORDER BY a.article_id DESC
						LIMIT %s',
						'Modules_News_Models_Article',
						$this->_conn->escape($lang),
						$this->_conn->escape($this->_lang),
						($selected) ? ' AND a.article_id = "' . $this->_conn->escape($selected) . '"' : null,
						$this->_conn->escape($count));
		$rs = $this->_conn->query($sql);
		$rows = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$rows[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $rows;
	}
	
	public function getSource($article)
	{
		$sql = sprintf("SELECT a.* 
						FROM " . $this->_conn->_tablePrefix . $this->_table . " AS a
						INNER JOIN " . $this->_conn->_tablePrefix . "core_translation AS tr
							ON a.article_id = tr.source_item_id
						WHERE tr.item_id = '%s' AND tr.item_class = '%s'
						LIMIT 1", 
						mysql_real_escape_string($article['article_id']),
						'Modules_News_Models_Article');
						
		$rs = $this->_conn->query($sql);
		$return = (0 == $this->_conn->count($rs)) ? null : $this->_conn->fetchAll($rs);
		$this->_conn->freeResult($rs);
		return $return;
	}
}